(function ($) {
  var fnName = "pop";
  jv.initMyPlugin(fnName, create);
  jv[fnName] = function (p, a, b, c) {

    p.id = p.id || "pop";

    var $pop = $(document.body).find("#" + p.id);
    if (!$pop.length) {
      $pop = $(document.body).append("<div class='pop' id='" + p.id + "'></div>").find("#" + p.id);
    }

    $pop[fnName](p, a, b, c);
  };

  function create(target) {
    var $this = $(target),
      p = $this.data(fnName);


    if (!p.data || !p.data.length) return;

    if ($this.hasClass("popEnum")) {
      return $this.show();
    }
    else {
      $this.addClass("popEnum").append("<div class='bankblock'></div><ul class='list fixed'></ul>");
    }


    $this.find(".list").empty().append(p.data.map(function (it) {
      return "<li dbId='" + it[p.idKey] + "'>" + it[p.nameKey] + "</li>";
    }));

    $this.bind("click", function (ev) {
      $(ev.target).closest(".pop").hide();
    });

    $this.find(".list").bind("click", function (ev) {
      var li = ev.target;
      if (li.tagName != "LI") {
        ev.stopPropagation();
        return false;
      }

      p.changed({id: li.getAttribute("dbId"), name: li.innerHTML});
    });
  }


  $.fn[fnName].defaults = {
    id: "",
    title: "",
    idKey: "id",
    nameKey: "name",
    current: "",
    changed: false,
  };

  $.fn[fnName].methods = {
    options: function ($this) {
      return $this.data(fnName);
    },
    show: function (target) {
      return $(target).show();
    }
  };


})($);

//--------------------------------------------------------------
